﻿Imports System.Drawing
Public Class TZMatrix
    Dim _tzs As System.Collections.Hashtable

    Private Sub TZMatrix_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dgvTZMatrix.Enabled = True
        dgvTZMatrix.CurrentCell = Nothing
        dgvTZMatrix.RowHeadersVisible = False
        dgvTZMatrix.AllowUserToOrderColumns = False
        dgvTZMatrix.AllowUserToResizeColumns = False
        dgvTZMatrix.AllowUserToResizeRows = False
        dgvTZMatrix.AllowUserToAddRows = False
        dgvTZMatrix.AllowUserToDeleteRows = False
        'dgvTZMatrix.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(231, 239, 255)

        dgvTZMatrix.SelectionMode = Windows.Forms.DataGridViewSelectionMode.FullRowSelect
        dgvTZMatrix.DefaultCellStyle.SelectionBackColor = Drawing.Color.Transparent
        dgvTZMatrix.DefaultCellStyle.SelectionForeColor = My.Settings.selectedRowFontColor

        dgvTZMatrix.ShowCellToolTips = False
       
        ShowMatrix(_tzs, Now)
    End Sub
    Sub ShowMatrix(ByVal tzs As System.Collections.Hashtable, ByVal referenceDate As Date)


        AddAppointmentTimeZoneColumns(tzs)

        Dim i As Integer
        For i = 0 To dgvTZMatrix.Columns.Count - 1
            dgvTZMatrix.Columns.Item(i).SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
            dgvTZMatrix.Columns(i).Width = 60
        Next i



        Dim newRec(dgvTZMatrix.Columns.Count * 2) As String
        Dim iterateTime As DateTime
        Dim temp As DateTime
        Dim workHoursStart As DateTime
        Dim workHoursEnd As DateTime

        With referenceDate
            iterateTime = New DateTime(.Year, .Month, .Day, 7, 0, 0)
            workHoursStart = New DateTime(.Year, .Month, .Day, 7, 0, 0)

            workHoursEnd = New DateTime(.Year, .Month, .Day, 18, 0, 0)
        End With
        'temp = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(temp, cApp.StartTimeZone.ID)

        Dim j As Integer
        Dim newrow As Integer
        Dim newRecHighlight(dgvTZMatrix.Columns.Count) As System.Drawing.Color
        Try
            For i = 1 To 24
                With dgvTZMatrix.Rows
                    For j = 0 To dgvTZMatrix.Columns.Count - 1
                        temp = iterateTime
                        If dgvTZMatrix.Columns(j).HeaderText <> "" Then
                            temp = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(temp, dgvTZMatrix.Columns(j).Name)
                            workHoursStart = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(temp, dgvTZMatrix.Columns(j).Name)
                            workHoursStart = workHoursStart.Parse(temp.ToString(System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern) & " 7:00 am")

                            workHoursEnd = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(temp, dgvTZMatrix.Columns(j).Name)
                            workHoursEnd = workHoursStart.Parse(temp.ToString(System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern) & " 6:00 pm")
                            If (temp >= workHoursStart) And (temp <= workHoursEnd) Then
                                'dgvTZMatrix.Rows(newrow).Cells(0).Style.BackColor = System.Drawing.Color.FromArgb(231, 239, 255)
                                newRecHighlight(j) = My.Settings.workHoursBackgroundColor 'System.Drawing.Color.FromArgb(231, 239, 255)
                                newRecHighlight(j + 1) = My.Settings.workHoursBackgroundColor ' System.Drawing.Color.FromArgb(231, 239, 255)
                            Else
                                newRecHighlight(j) = System.Drawing.Color.White
                                newRecHighlight(j + 1) = System.Drawing.Color.White
                            End If
                            newRec(j) = temp.ToString("ddd")
                            newRec(j + 1) = temp.ToString("hh tt")
                            If (newRec(j + 1) = "12 PM") Then
                                newRec(j + 1) = "noon"
                            End If
                            If (newRec(j + 1) = "12 AM") Then
                                newRec(j + 1) = "midnight"
                            End If

                        End If

                    Next j
                    newrow = .Add(newRec)
                    dgvTZMatrix.Rows(newrow).Height = dgvTZMatrix.Rows(newrow).Height - 4
                    For j = 0 To dgvTZMatrix.Columns.Count - 1
                        dgvTZMatrix.Rows(newrow).Cells(j).Style.BackColor = newRecHighlight(j)
                        dgvTZMatrix.Rows(newrow).Cells(j).Style.ForeColor = My.Settings.workHoursFontColor

                        If (dgvTZMatrix.Rows(newrow).Cells(j).Value = "noon") Then
                            dgvTZMatrix.Rows(newrow).Cells(j).Style.ForeColor = My.Settings.noonFontColor
                            dgvTZMatrix.Rows(newrow).Cells(j).Style.BackColor = My.Settings.noonBackgroundColor

                        End If
                        If dgvTZMatrix.Rows(newrow).Cells(j).Value = "midnight" Then
                            dgvTZMatrix.Rows(newrow).Cells(j).Style.ForeColor = My.Settings.noonFontColor
                            dgvTZMatrix.Rows(newrow).Cells(j).Style.BackColor = My.Settings.noonBackgroundColor
                        End If
                    Next
                    iterateTime = DateAdd(DateInterval.Hour, 1, iterateTime)
                End With
            Next
            dgvTZMatrix.ClearSelection()
        Catch ex As Exception
            InternationalTimes.ShowError(ex.Message, ex.StackTrace)
        End Try
    End Sub

   
    Sub AddAppointmentTimeZoneColumns(ByVal tzs As System.Collections.Hashtable)
        Dim colIndex As Integer

        Me.dgvTZMatrix.Columns.Clear()
        Dim Enumerator As System.Collections.IDictionaryEnumerator
        Enumerator = tzs.GetEnumerator()

        While Enumerator.MoveNext()
            colIndex = Me.dgvTZMatrix.Columns.Add(Enumerator.Key, Enumerator.Value)
            dgvTZMatrix.Columns(colIndex).Tag = 2
            colIndex = Me.dgvTZMatrix.Columns.Add("", "")
            dgvTZMatrix.Columns(colIndex).Tag = 1
        End While

        PrepareSpanValue()

    End Sub



    Private Sub TimeZonesPage_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        'ShowTZMatrix()

    End Sub

    Private Sub dgvTZMatrix_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTZMatrix.CellMouseEnter

        dgvTZMatrix.ClearSelection()
        If e.RowIndex >= 0 Then
            dgvTZMatrix.Rows(e.RowIndex).Selected = True
        End If
    End Sub
    Sub PrepareSpanValue()
        Dim col As System.Windows.Forms.DataGridViewColumn
        For Each col In dgvTZMatrix.Columns
            If col.Tag > 1 Then
                For i = 1 To col.Tag - 1
                    dgvTZMatrix.Columns(col.Index + i).Tag = -1

                Next
            End If
        Next

    End Sub


    Private Sub dgvTZMatrix_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgvTZMatrix.CellPainting

        If e.RowIndex = -1 Then
            Dim j As Integer
            'todo: fix runtime potential issue here
            Select Case dgvTZMatrix.Columns(e.ColumnIndex).Tag
                Case Is > 1
                    dgvTZMatrix.ColumnHeadersBorderStyle = Windows.Forms.DataGridViewHeaderBorderStyle.Raised


                    Dim w2 As Integer = 0
                    Dim r1 As System.Drawing.Rectangle = dgvTZMatrix.GetCellDisplayRectangle(e.ColumnIndex, -1, True)

                    For j = 2 To dgvTZMatrix.Columns(e.ColumnIndex).Tag
                        w2 = w2 + dgvTZMatrix.GetCellDisplayRectangle(e.ColumnIndex + (j - 1), -1, True).Width
                    Next
                    'w2 = dgvTZMatrix.GetCellDisplayRectangle(e.ColumnIndex + 1, -1, True).Width

                    'r1.Height -= 1
                    r1.X -= 1
                    r1.Width = r1.Width + w2 + 1 '+ w2 '- 2
                    e.Graphics.FillRectangle(New System.Drawing.SolidBrush(dgvTZMatrix.ColumnHeadersDefaultCellStyle.BackColor), _
                                             r1)
                    'e.Graphics.DrawRectangle(Drawing.Pens.Black, r1)
                    DrawBorder(e.Graphics, r1, Windows.Forms.BorderStyle.Fixed3D, False)

                    Dim format As System.Drawing.StringFormat = New System.Drawing.StringFormat()
                    format.Alignment = System.Drawing.StringAlignment.Center
                    format.LineAlignment = System.Drawing.StringAlignment.Center


                    e.Graphics.DrawString(dgvTZMatrix.Columns(e.ColumnIndex).HeaderText, _
                            dgvTZMatrix.ColumnHeadersDefaultCellStyle.Font, _
                            New System.Drawing.SolidBrush(dgvTZMatrix.ColumnHeadersDefaultCellStyle.ForeColor), _
                            r1, format)
                    e.Handled = True
                Case Is < 0
                    e.Handled = True
            End Select

        End If

    End Sub
    Private Sub DrawBorder(ByVal gr As Graphics, ByVal rect As  _
    Rectangle, ByVal border_style As System.Windows.Forms.BorderStyle, Optional _
    ByVal sunken As Boolean = True)
        Select Case border_style
            Case BorderStyle.FixedSingle
                rect.Width -= 1
                rect.Height -= 1
                gr.DrawRectangle(Pens.Black, rect)
            Case BorderStyle.Fixed3D
                Dim colors() As Color
                If sunken Then
                    colors = New Color() { _
                        SystemColors.ControlDark, _
                        SystemColors.ControlDarkDark, _
                        SystemColors.ControlLightLight, _
                        SystemColors.ControlLight _
                    }
                Else
                    colors = New Color() { _
                        SystemColors.ControlLightLight, _
                        SystemColors.ControlLight, _
                        SystemColors.ControlDark, _
                        SystemColors.ControlDarkDark _
                    }
                End If
                Dim p As Pen
                p = New Pen(colors(0))
                gr.DrawLine(p, rect.X, rect.Y + rect.Height - _
                    1, rect.X, rect.Y)
                gr.DrawLine(p, rect.X, rect.Y, rect.X + _
                    rect.Width - 1, rect.Y)
                p = New Pen(colors(1))
                gr.DrawLine(p, rect.X + 1, rect.Y + rect.Height _
                    - 2, rect.X + 1, rect.Y + 1)
                gr.DrawLine(p, rect.X + 1, rect.Y + 1, rect.X + _
                    rect.Width - 2, rect.Y + 1)
                p = New Pen(colors(2))
                gr.DrawLine(p, rect.X, rect.Y + rect.Height - _
                    1, rect.X + rect.Width - 1, rect.Y + _
                    rect.Height - 1)
                gr.DrawLine(p, rect.X + rect.Width - 1, rect.Y _
                    + rect.Height - 1, rect.X + rect.Width - 1, _
                    rect.Y)
                p = New Pen(colors(3))
                gr.DrawLine(p, rect.X + 1, rect.Y + rect.Height _
                    - 2, rect.X + rect.Width - 2, rect.Y + _
                    rect.Height - 2)
                gr.DrawLine(p, rect.X + rect.Width - 2, rect.Y _
                    + rect.Height - 2, rect.X + rect.Width - 2, _
                    rect.Y + 1)
        End Select
    End Sub

    Public Sub New(ByVal tzs As System.Collections.Hashtable)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _tzs = tzs
    End Sub
End Class
